# 11_jes2016_wave13.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2020-5-16 Taka-aki Asano
# Last Modified: 2021-10-9

# package
require("haven")
require("dplyr")
require("ltm")
require("plink")


# dataset
JES2016 <- read_sav(
  "JESV第13波14波2016年参院選統合データ.sav", 
  user_na = FALSE
)


# respondent
JES2016_Respondent <- JES2016[,c("id", paste0("Q", 11:20, "_1"))]
colnames(JES2016_Respondent)[-1] <- paste0("q", 11:20)


# IRT
## handle missing data
JES2016_Respondent <- JES2016_Respondent[rowSums(JES2016_Respondent[,-1], na.rm = TRUE) != 0,]

## estimate item parameters
irt2016 <- ltm::grm(JES2016_Respondent[,-1], IRT.param = FALSE)
irt2016 ## viewing

## estimate voters ideology
score2016_Respondent <- ltm::factor.scores(irt2016, JES2016_Respondent[,-1])
JES2016_Respondent$Ideology <- -1 * score2016_Respondent$score.dat$z1


# rescale
## common item
common2012 <- c("q8", "q9", "q10", "q11", "q12", "q13")
common2016 <- c("q13", "q14", "q15", "q16", "q17", "q18")
index <- data.frame(
  group1 = match(common2012, colnames(JES2012_Respondent[,-1])), 
  group2 = match(common2016, colnames(JES2016_Respondent[,-1]))
)

## list of parameters
### 2012
res2012 <- coef(irt2012)
res2012 <- res2012[,c(4,1:3)]
colnames(res2012) <- c("a","b1","b2","b3")
### 2016
res2016 <- coef(irt2016)
res2016 <- res2016[,c(4,1:3)]
colnames(res2016) <- c("a","b1","b2","b3")
### merge
pm <- list(res2012, res2016)

## list of scale
rescat <- list(rep(4, nrow(res2012)), 
               rep(4, nrow(res2016)))

## as.irt.pars
pm2012 <- as.poly.mod(n = nrow(res2012), model = "grm", 
                      items = 1:nrow(res2012))
pm2016 <- as.poly.mod(n = nrow(res2016), model = "grm", 
                      items = 1:nrow(res2016))
res <- as.irt.pars(pm, common = index, cat = rescat, 
                   poly.mod = list(pm2012, pm2016), 
                   location = FALSE)

## list of voters' position
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2016_Respondent$score.dat$z1)

## rescale
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
summary(plink_out)

## estimate voters ideology (rescale)
score2016_Respondent_plink <- link.ability(plink_out)$group2
JES2016_Respondent$Ideology <- -1 * score2016_Respondent_plink


# LDP's position
JES2016_LDP <- JES2016[,c("id", paste0("Q", 11:20, "_3_1"))]
colnames(JES2016_LDP)[-1] <- paste0("q", 11:20)
JES2016_LDP <- JES2016_LDP[rowSums(JES2016_LDP[,-1], na.rm = TRUE) != 0,]
score2016_LDP <- ltm::factor.scores(irt2016, JES2016_LDP[,-1])
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2016_LDP$score.dat$z1)
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
JES2016_LDP$Ideology <- -1 * link.ability(plink_out)$group2


# DPJ's position
JES2016_DPJ <- JES2016[,c("id", paste0("Q", 11:20, "_3_2"))]
colnames(JES2016_DPJ)[-1] <- paste0("q", 11:20)
JES2016_DPJ <- JES2016_DPJ[rowSums(JES2016_DPJ[,-1], na.rm = TRUE) != 0,]
score2016_DPJ <- ltm::factor.scores(irt2016, JES2016_DPJ[,-1])
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2016_DPJ$score.dat$z1)
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
JES2016_DPJ$Ideology <- -1 * link.ability(plink_out)$group2
